package alf.mapping.view.activity;
import alf.common.util.SimpleFileNameFilter;
import alf.mapping.view.Env;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;

public class FileSelectionActivity extends SimpleAbstractActivity {
	private FileListAdapter adapter;
	private int idx= -1;
	private File lastFileSel;
	private ListView lista;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file_selection);
		
		String defaultFileName = getIntent().getExtras().getString(Intent.EXTRA_TEXT);
		
		setSelectedFilename(defaultFileName);
		lastFileSel = new File(defaultFileName);
		
		lista = (ListView) findViewById(R.id.file_list);
		adapter = new FileListAdapter(this);
		adapter.setPath(defaultFileName);
		lista.setAdapter(adapter);
		lista.setOnItemClickListener(new ListView.OnItemClickListener(){
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
					selecciona(position);
				}
			});
	}

	private void setSelectedFilename(String defaultFileName) {
		EditText view = (EditText) this.findViewById(R.id.file_path);
		view.setText(defaultFileName, EditText.BufferType.EDITABLE);
	}
	
	private void selecciona(int position) {
		lastFileSel = (File) adapter.getItem(position);
		String fsel = lastFileSel.getPath();
		setSelectedFilename(fsel);
		adapter.setPath(fsel);
		lista.setAdapter(adapter);
		idx = position;
	}
	
	public void onOk(View v) {
		if (!lastFileSel.isDirectory()){
		Intent i = getIntent();
		EditText view = (EditText) this.findViewById(R.id.file_path);
		i.putExtra(Intent.EXTRA_RETURN_RESULT,view.getText().toString() );
		setResult(RESULT_OK, i);
		finish();
		} else {
			Env.toast(this,"Cannot open a directory");
		}
	}
	
	class FileListAdapter extends BaseAdapter {
		File base;
		File[] fileList;
		Context ctx;
		
		FileListAdapter(Context ctx) {
			this.ctx = ctx;
		}
		void setPath(String path) {
			base = new File(path);
			if (!base.isDirectory()){
				base = base.getParentFile();
			}
			File[] list = getFiles(base);
			fileList = new File[list.length+1];
			fileList[0] = base.getParentFile();
			for (int i=0;i<list.length;i++){
				fileList[i+1] = list[i];
			}
		}

		private File[] getFiles(File base) {
			SimpleFileNameFilter filter = new  SimpleFileNameFilter();
			filter.setExt("xml");
			filter.setAccepDirs(true);
			return base.listFiles(filter);
		}
		
		public int getCount() {
			return fileList.length;
		}

		public Object getItem(int p1) {
			// TODO: Implement this method
			return fileList[p1];
		}

		public long getItemId(int p1) {
			// TODO: Implement this method
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tView;
			if (convertView == null) {  // if it's not recycled, initialize some attributes
				tView = new TextView(ctx);
				AbsListView.LayoutParams layoutParams = new ListView.LayoutParams(
					ListView.LayoutParams.FILL_PARENT,
					ListView.LayoutParams.WRAP_CONTENT);
					
				tView.setLayoutParams(layoutParams);
	
			} else {
				tView = (TextView) convertView;
			}
			String text;
			if (position==0){
				text = "..";
			} else {
				text = fileList[position].getName();
			}
			tView.setText(text);
			return tView;
		}
		
	}
}
